package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.ResourcesService;
import com.yixun.qfbao.apidto.GetOptAuthReqDto;
import com.yixun.qfbao.apidto.GetOptAuthResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetOptAuthReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 获取用户操作权限
 * @author guifang.weng
 * @create 2017-11-23 10:59
 **/
@Service
public class GetOptAuthHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetOptAuthHander.class);

    @Autowired
    private ResourcesService resourcesService;
    @Autowired
    private LoginUtils loginUtils;
    @Autowired
    private PartnerInfoService partnerInfoService;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<GetOptAuthResDto> res = new BaseResponseDTO<>();
        GetOptAuthReqVo reqVo = (GetOptAuthReqVo)reqDto;
        GetOptAuthReqDto getOptAuthReqDto = reqVo.getParams();
        try {
        	if(StringUtils.isNotBlank(getOptAuthReqDto.getUserId())){
        		PartnerInfo partner = partnerInfoService.selectById(Long.parseLong(getOptAuthReqDto.getUserId()));
        		getOptAuthReqDto.setAccountType(partner.getAccountType());
                getOptAuthReqDto.setRoleId(partner.getUserRoleId());
                GetOptAuthResDto resDto = resourcesService.getOptAuth(getOptAuthReqDto);
                res.setResult(resDto.getResult());
                res.setResData(resDto);
        	}else{
                res.setResult(ResultEnum.FAIL.getNo());
                res.setErrorMsg("获取用户操作权限失败");
            }

        } catch (Exception e) {
            logger.error("GetOptAuthHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("获取用户操作权限失败");
        }
        return res;
    }
}
